<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CouponCode;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Product;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use App\Models\CouponCode as CouponCodeModel;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CouponCodeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CouponCode(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->name;
            $grid->code;
            $grid->description;
            $grid->type->display(function ($value) {
                return CouponCodeModel::$typeMap[$value];
            });
            $grid->value;
            $grid->column('usage', '用量')->display(function ($value) {
                return "{$this->used} / {$this->total}";
            });
            $grid->min_amount;
            $grid->not_before;
            $grid->not_after;
            $grid->enabled->display(function ($value) {
                return $value ? "是" : "否";
            });
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->actions(function ($actions) {
               $actions->disableView();
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    // 优惠券限定使用范围
    public function ableType(Request $request)
    {
        $type = $request->get('q');

        switch ($type) {
            case Category::class :
                $data = Category::query()->where('is_directory', true)->get(['id', DB::raw('name as text')]);
                break;
            case Product::class :
                $data = Product::query()->get(['id', DB::raw('title as text')]);
                break;
            case Brand::class :
                $data = Brand::query()->get(['id', DB::raw('name as text')]);
                break;
            default:
                $data = null;
                break;
        }

        return $data;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CouponCode(), function (Form $form) {
            $form->display('id');
            $form->text('name')->rules('required')->required();
            $form->text('code')->rules(function ($form) {
                if (!$id = $form->model()->id) {
                    return 'nullable|unique:coupon_codes';  // 新增
                } else {
                    return 'nullable|unique:coupon_codes,code,' . $id . ',id'; //编辑
                }
            });
            $form->radio('type')->options(CouponCodeModel::$typeMap)->rules('required')
                ->required()->default(CouponCodeModel::TYPE_FIXED);
            $form->text('value')->rules(function ($form) {
                if (request()->input('type') === CouponCodeModel::TYPE_PERCENT) {
                    // 如果是折扣类型, 那么折扣范围只能是 1 ~ 99
                    return 'required|numeric|between:1,99';
                } else {
                    // 否则只要大于0.01即可
                    return 'required|numeric|min:0.01';
                }
            });

            $form->select('coupon_codeable_type')->options(\App\Models\CouponCode::$couponCodeAbleMap)
                ->load('coupon_codeable_id', 'api/able-type')->help('选填: 不填写表示全场优惠劵');

            $form->select('coupon_codeable_id')->help('选填: 必须先选择"优惠券适用范围"');

            $form->text('total')->rules('required|numeric|min:0');
            $form->text('min_amount')->rules('required|numeric|min:0');
            $form->datetime('not_before');
            $form->datetime('not_after');
            $form->radio('enabled')->options(['1' => '是', '0' => '否'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                if (!$form->code) {
                    $form->code = CouponCodeModel::findAvailableCode();
                }
            });
        });
    }
}
